﻿using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using PortalWeb.CMS.Business;
using PortalWeb.CMS.Core.Entity;
using PortalWeb.Models.Cms;

namespace PortalWeb.Controllers.Cms
{
    public class ProductController : Controller
    {

        public ProductController(ProductManager productManager)
        {
            ProductManager = productManager;
        }

        public ProductManager ProductManager { get; private set; }

        //
        // GET: /Products/
        public async Task<ActionResult> Index(int? categoryId)
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
            int offset = pageSize*(pageIndex - 1);
            int limint = pageSize*pageIndex;
            //调用分页的方法，传递参数,拿到分页之后的数据
            var data = from cm in await ProductManager.LoadPageEntitiesAsync(
                u => categoryId == null || u.CommonModel.Category.Id == categoryId, true, u => u.Id, offset, limint)
                select new ProductViewModel()
                {
                    Id = cm.Id,
                    Intro = cm.Intro,
                    Content = cm.Content,
                    Title = cm.CommonModel.Title,
                    CategoryName = cm.CommonModel.Category.Name,
                    ReleaseDate = cm.CommonModel.ReleaseDate
                };

            return View(data.ToList());
        }

        public async Task<ActionResult> SimpleIndex(int? categoryId)
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 3 : int.Parse(Request["rows"]);
            int offset = pageSize*(pageIndex - 1);
            int limint = pageSize*pageIndex;
            //调用分页的方法，传递参数,拿到分页之后的数据
            var data =
                from product in await ProductManager.LoadPageEntitiesAsync(
                    u => categoryId == null || u.CommonModel.Category.Id == categoryId, true, u => u.Id, offset,
                    limint)
                select new ProductViewModel()
                {
                    Id = product.Id,
                    CategoryId = product.CommonModel.CategoryId,
                    Brand = product.Brand,
                    Price = product.Price,
                    Status = product.CommonModel.Status,
                    Title = product.CommonModel.Title,
                    ReleaseDate = product.CommonModel.ReleaseDate,
                    Inputer = product.CommonModel.Inputer,
                    Content = product.Content,
                    Intro = product.Intro,
                    DefaultPicUrl = product.CommonModel.DefaultPicUrl,
                };
            return View(data.ToList());
        }

        //
        // GET: /News/Details/5
        public ActionResult Details(int id)
        {
            Product product = ProductManager.Get(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(new ProductViewModel(product));
        }
    }
}